//
//  JZAddManagerViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/21.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "JZAddManagerViewController.h"

#import "SPManagerCell.h"
#import "MessViewController.h"
#import "AddJZManagerViewController.h"
#import "JZDetViewController.h"
#import "JZUpdateViewController.h"
@interface JZAddManagerViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *urlStr;
    UIView *view2;
    NSMutableArray *array;
}
@property (nonatomic,strong)UITableView *tableView;

@end

@implementation JZAddManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBGColor];
    [self showNav:@"商品管理"];
    array = [NSMutableArray array];
    urlStr = @[@"",@"/App/Bridalchamber/management",@"/Merchantregulator/buyacar",@"/App/Merchantregulator/cleaning",@"",@"/App/Merchantregulator/vegetables",@"/App/Merchantregulator/coolie",@"",@"/App/Merchantregulator/housekeeping",@"/App/Merchantregulator/carwash"];
    [self loadUI];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadData];
}

- (void)loadUI{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, ScreenHeight-50, ScreenWidth, 50);
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"    添加商品" forState:UIControlStateNormal];
    [btn setTitleColor:COLOR_RGB(65, 132, 255, 1) forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"矩形-21-拷贝"] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [btn addTarget:self action:@selector(clickAdd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-50) style:UITableViewStylePlain];
    _tableView.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-50)];
    view2.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    view2.hidden = YES;
    [self.view addSubview:view2];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 100, 21)];
    lab.text = @"暂无数据。。。";
    [view2 addSubview:lab];
    
}

- (void)clickAdd{
    AddJZManagerViewController *kl = [[AddJZManagerViewController alloc] init];
    kl.merid = _merid;
    [self.navigationController pushViewController:kl animated:YES];
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"CellId";
    SPManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SPManagerCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.btnUpdate.layer.cornerRadius = 5.0;
    cell.btnUpdate.layer.borderWidth = 0.5;
    cell.btnUpdate.layer.borderColor = [UIColor redColor].CGColor;
    
    cell.btnDelete.layer.cornerRadius = 5.0;
    cell.btnDelete.layer.borderWidth = 0.5;
    cell.btnDelete.layer.borderColor = [UIColor redColor].CGColor;
    
    cell.btnBH.layer.cornerRadius = 5.0;
    cell.btnBH.layer.borderWidth = 0.5;
    cell.btnBH.layer.borderColor = [UIColor redColor].CGColor;
    
    NSDictionary *op = array[indexPath.row];
    
    cell.labName.text = [op objectForKey:@"classname"];
    cell.labDet.text = [op objectForKey:@"synopsis"];
    NSString *date = [op objectForKey:@"add_time"];
    cell.labDate.text = [self getNewDate:date];
    
    cell.labPrice.text = [NSString stringWithFormat:@"￥%@",[op objectForKey:@"price"]];
    
    NSString *order = [NSString stringWithFormat:@" %li",[[op objectForKey:@"order"] integerValue]];
    [cell.btnOrder setTitle:order forState:UIControlStateNormal];
    //5回收，4未审核，3驳回，2通过
    NSString *audit = [NSString stringWithFormat:@"%li",[[op objectForKey:@"audit"] integerValue]];
    //产品id
    NSString *productid = [NSString stringWithFormat:@"%li",[[op objectForKey:@"id"] integerValue]];
    
    //2、下架 3、上架
    NSString *isdisplay = [NSString stringWithFormat:@"%li",[[op objectForKey:@"isdisplay"] integerValue]];
    //是否上下架 2是3否
    if ([isdisplay integerValue] == 2) {
        [cell.btnSwit setImage:[UIImage imageNamed:@"下架12312"] forState:UIControlStateNormal];
    }else{
        [cell.btnSwit setImage:[UIImage imageNamed:@"上架12312"] forState:UIControlStateNormal];
    }
    
     //商品状态,5回收，4未审核，3驳回，2通过
    if ([audit integerValue] == 2) {
        cell.btnBH.hidden = YES;
        
    }else if ([audit integerValue] == 3){
        cell.btnDelete.hidden = NO;
        cell.btnUpdate.hidden = NO;
        cell.btnSwit.hidden = YES;
        cell.btnBH.hidden = NO;
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 130, 60, 20)];
        lab.text = @"被驳回";
        lab.font = [UIFont systemFontOfSize:14.0];
        [cell.contentView addSubview:lab];
    }else if ([audit integerValue] == 4){
        cell.btnDelete.hidden = YES;
        cell.btnUpdate.hidden = YES;
        cell.btnSwit.hidden = YES;
        cell.btnBH.hidden = YES;
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 130, 60, 20)];
        lab.text = @"审核中";
        lab.font = [UIFont systemFontOfSize:14.0];
        [cell.contentView addSubview:lab];
        
    }else if ([audit integerValue] == 5){
        
    }
    
    //上下架
    cell.left = ^{
        
        [self loadSJ:productid];
    };
    //驳回
    cell.bohui = ^{
        [self loadMess];
    };
    //修改
    cell.book = ^{
        [self loadUpdate:op];
    };
    //删除
    cell.right = ^{
        [self loadDel:productid];
    };
    
    
    return cell;
}

//上架
- (void)loadSJ:(NSString *)productid{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:productid forKey:@"productid"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/undercarriage",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            [self loadData];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)loadMess{
    MessViewController *mess = [[MessViewController alloc] init];
    [self.navigationController pushViewController:mess animated:YES];
}
//修改
- (void)loadUpdate:(NSDictionary *)dic{
    JZUpdateViewController *update = [[JZUpdateViewController alloc] init];
    update.dic = dic;
    update.merid = _merid;
    [self.navigationController pushViewController:update animated:YES];
}
//删除
- (void)loadDel:(NSString *)productId{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否删除该商品" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
        [parmas setObject:productId forKey:@"productid"];
        NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/del",urlHead];
        [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"删除res:%@",responseObject);
            NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
            if ([error integerValue] == 200) {
                [self loadData];
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"error:%@",error);
        }];
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
    
}
//cell height
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JZDetViewController *det = [[JZDetViewController alloc] init];
    NSDictionary *dic = array[indexPath.row];
    det.dic = dic;
    [self.navigationController pushViewController:det animated:YES];
}

//商家管理-家政
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    NSInteger tags = [_merType integerValue];
    NSString *url = [NSString stringWithFormat:@"%@%@",urlHead,urlStr[tags]];
    NSLog(@"url:%@",url);
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"家政-res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            _tableView.hidden = NO;
            view2.hidden = YES;
            array = [responseObject objectForKey:@"result"];
            [_tableView reloadData];
        }else{
            _tableView.hidden = YES;
            view2.hidden = NO;
        }
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
